package com.wb.mas.binding.guideviewpage;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.databinding.BindingAdapter;

import com.wb.mas.R;
import com.wb.mas.entity.GuideEntity;

import java.util.List;

import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.widget.banner.MZBannerView;
import me.goldze.mvvmhabit.widget.banner.holder.MZHolderCreator;
import me.goldze.mvvmhabit.widget.banner.holder.MZViewHolder;

/**
 * Copyright (C) 2018,
 * ViewAdapter
 * <p>
 * Description
 * 引导页面viewPager的适配器
 *
 * @author mwl
 * @version 1.0
 * <p>
 * Ver 1.0, 2019-04-28, yue, Create file
 */
public class ViewAdapter {

    @BindingAdapter(value = {"itemData", "onGoClick"}, requireAll = false)
    public static void onInitDatasCommand(final MZBannerView mzBannerView,
                                          final List<GuideEntity> items,
                                          final BindingCommand onGoClick) {
        mzBannerView.setPages(items, new MZHolderCreator<ViewPagerHolder>() {
            @Override
            public ViewPagerHolder createViewHolder() {
                return new ViewPagerHolder(onGoClick);
            }
        });
    }


    public static final class ViewPagerHolder implements MZViewHolder<GuideEntity> {
        private ImageView mPic;
        private TextView mTitle;
        private TextView mContent;
        private ImageView mGo;

        private BindingCommand onGoClick;

        public ViewPagerHolder(BindingCommand onGoClick) {
            this.onGoClick = onGoClick;
        }

        @Override
        public View createView(Context context) {
            View view = LayoutInflater.from(context).inflate(R.layout.recycle_guide_viewpager, null);
            mPic = view.findViewById(R.id.iv_pic);
            mTitle = view.findViewById(R.id.tv_title);
            mContent = view.findViewById(R.id.tv_content);
            mGo = view.findViewById(R.id.iv_go);
            return view;
        }

        @Override
        public void onBind(Context context, int position, GuideEntity data) {
            mPic.setImageResource(data.getImageResource());
            mTitle.setText(data.getTitle());
            mContent.setText(data.getContent());
            if (data.isShowGo()) {
                mGo.setVisibility(View.VISIBLE);
                mGo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onGoClick != null) {
                            onGoClick.execute();
                        }
                    }
                });
            } else {
                mGo.setVisibility(View.GONE);
            }

        }
    }
}
